<template>
  <div class="main">
    <section>
      <el-form :model="form" ref="form" label-width="120px" class="demo-ruleForm">
        <el-form-item label="属性组名称:" prop="parentName">
          <el-input disabled :maxlength="32" v-model="form.parentName" placeholder="请输入属性组名称"></el-input>
        </el-form-item>
        <el-form-item label="属性名称:" prop="name" :rules="commonRules.notNullRule">
          <el-input :maxlength="32" v-model="form.name" placeholder="请输入属性名称"></el-input>
        </el-form-item>
        <el-form-item label="录入方式:" prop="inputType" :rules="commonRules.notNullRule">
          <el-radio v-model="form.inputType" :label="0">手工输入</el-radio>
          <el-radio v-model="form.inputType" :label="1">列表选取</el-radio>
        </el-form-item>
        <template v-if="form.inputType === 1">
          <el-form-item label="可选值列表:" prop="inputs" :rules="commonRules.notNullRule">
            <el-input type="textarea" :maxlength="250" :rows="5" placeholder="请填写属性值,多个请用英文','分隔" v-model="form.inputs">
            </el-input>
          </el-form-item>
          <el-form-item label="是否支持检索:" prop="searchType" :rules="commonRules.notNullRule">
            <el-radio v-model="form.searchType" :label="1">是</el-radio>
            <el-radio v-model="form.searchType" :label="0">否</el-radio>
          </el-form-item>
        </template>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" :min="0" v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submitForm">保存</el-button>
        </el-form-item>
      </el-form>
    </section>
  </div>
</template>

<script>
import { insetrAttr, updateAttr } from '@/api/goodMiddleStage'

export default {
  name: 'brand',
  data() {
    return {
      loading: false,
      form: {
        parentName: '',
        attrGroupId: 0,
        inputType: 0,
        inputs: '',
        name: '',
        searchType: 0,
        selectType: 0,
        sort: 0,
        type: 0
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.form = { ...this.form, ...this.$route.params }

      this.form.inputs = this.form.inputs && this.form.inputs.join(',')
    },
    submitForm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            let data = ''
            let inputs = ''
            // 录入方式为手工输入时，可选值列表置空
            if (this.form.inputType === 0) {
              inputs = []
            } else {
              // 可选值输入 以逗号分隔
              inputs = this.form.inputs && this.form.inputs.split(',') || []
            }
            console.log('录入类型', inputs)
            if (this.$route.name === 'goodsMiddleStage-propertyDetailEdit') {
              data = await updateAttr({
                ...this.form,
                inputs
              })
            } else {
              data = await insetrAttr({
                ...this.form,
                inputs
              })
            }
            if (data.message === 'SUCCESS') {
              this.$message.success('操作成功')
              this.loading = false
              this.$refs['form'].resetFields()
              this.$router.push({
                name: 'goodsMiddleStage-propertyDetail',
                params: { ...this.form }
              })
            } else {
              this.$message.success('操作失败')
            }
          } catch (error) {
            console.log(error)
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
</style>